﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "ElementUISwitch.h"

#include "Widget/SSwitch.h"

UElementUISwitch::UElementUISwitch()
{
	IsSwitch = false;
	IconWidth = 60;
	LeftLabelText = FText::FromString("");
	LeftLabelFont = FCoreStyle::GetDefaultFontStyle("Roboto", 18);
	RightLabelText = FText::FromString("");
	RightLabelFont = FCoreStyle::GetDefaultFontStyle("Roboto", 18);
	IconBrushColor = FColor(255, 255, 255);
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> IconMaterial(TEXT("Material'/ElementUI/Material/Common/M_Sphere.M_Sphere'"));
	if (IconMaterial.Succeeded())
	{
		IconBrush.SetResourceObject(IconMaterial.Object);
	}
}

void UElementUISwitch::SynchronizeProperties()
{
	Super::SynchronizeProperties();

	SwitchSlate->SetIconWidth(IconWidth);
	SwitchSlate->SetSwitchPosition(IsSwitch ? EHorizontalAlignment::HAlign_Right : EHorizontalAlignment::HAlign_Left);
	SwitchSlate->SetBorderImage(&IconBrush);
	SwitchSlate->SetLeftLabelText(LeftLabelText);
	SwitchSlate->SetLeftLabelFont(LeftLabelFont);
	SwitchSlate->SetRightLabelText(RightLabelText);
	SwitchSlate->SetRightLabelFont(RightLabelFont);
	SetIconBrushColor(IconBrushColor);
}

void UElementUISwitch::SetIconBrushColor(FColor value)
{
	IconBrushColor = value;
	UObject* Resource = IconBrush.GetResourceObject();
	if (!Resource) return;
	UMaterialInterface* Material = Cast<UMaterialInterface>(Resource);
	// GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Green, FString("1"));
	if (Material)
	{
		UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Material);
		// GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Green, FString("2"));
		if (!DynamicMaterial)
		{
			DynamicMaterial = UMaterialInstanceDynamic::Create(Material, this);
			DynamicMaterial->SetVectorParameterValue("BackgroundColor", IconBrushColor.ReinterpretAsLinear());
			IconBrush.SetResourceObject(DynamicMaterial);
			// GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Green, FString("3"));
			if (SwitchSlate.IsValid())
			{
				SwitchSlate->SetBorderImage(&IconBrush);
			}
		} else
		{
			// GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Green, FString("4"));
			DynamicMaterial->SetVectorParameterValue("BackgroundColor", IconBrushColor.ReinterpretAsLinear());
			// if (SwitchSlate.IsValid())
			// {
			// 	SwitchSlate->SetBorderImage(&IconBrush);
			// }
		}
	}
}

void UElementUISwitch::SetLeftLabelText(const FText& value)
{
	if (SwitchSlate.IsValid())
	{
		LeftLabelText = value;
		SwitchSlate->SetLeftLabelText(LeftLabelText);
	}
}

void UElementUISwitch::SetLeftLabelFont(const FSlateFontInfo& value)
{
	if (SwitchSlate.IsValid())
	{
		LeftLabelFont = value;
		SwitchSlate->SetLeftLabelFont(LeftLabelFont);
	}
}

void UElementUISwitch::SetRightLabelText(const FText& value)
{
	if (SwitchSlate.IsValid())
	{
		RightLabelText = value;
		SwitchSlate->SetRightLabelText(RightLabelText);
	}
}

void UElementUISwitch::SetRightLabelFont(const FSlateFontInfo& value)
{
	if (SwitchSlate.IsValid())
	{
		RightLabelFont = value;
		SwitchSlate->SetRightLabelFont(RightLabelFont);
	}
}

TSharedRef<SWidget> UElementUISwitch::RebuildWidget()
{
	SwitchSlate = SNew(SSwitch)
		.IconWidth(IconWidth)
		.SwitchPosition(IsSwitch ? EHorizontalAlignment::HAlign_Right : EHorizontalAlignment::HAlign_Left)
		.BorderImage(&IconBrush)
		.LeftLabelText(LeftLabelText)
		.LeftLabelFont(LeftLabelFont)
		.RightLabelText(RightLabelText)
		.RightLabelFont(RightLabelFont);
	return SwitchSlate.ToSharedRef();
}
